﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.OleDb;
using System.IO;

public partial class Manage_addDinner : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            BindDDlClass();
        }
    }
    protected void btnAdd_Click(object sender, EventArgs e)
    {
        int add;
        DinnerEles de = new DinnerEles();
        de.DinnerName = tbDName.Text.Trim();
        de.DinnerContent = tbDContent.Text.Trim();
        de.DinnerPrice = Convert.ToInt32(tbDPrice.Text.Trim());
        de.DinnerPicPath = UploadDocument();
        if (de.DinnerPicPath == null)
        { de.DinnerPicPath = ""; }
        de.DinnerClass = Convert.ToInt32(ddlClass.SelectedValue.ToString());
        Dining din = new Dining();
        add = din.AddDinner(de);
        if (add == 0)
        { SystemTools.PopMsg("ADD DINNER ERROR"); }
        else
        { SystemTools.PopMsg("ADD DINNER OK","addDinner.aspx"); }
    }

    private void BindDDlClass()
    { Dining din = new Dining();
    OleDbDataReader dr = din.GetDinnerClass();

    ddlClass.DataSource = dr;
    ddlClass.DataTextField = "DCName";
    ddlClass.DataValueField = "DCID";
    ddlClass.DataBind();
    dr.Close();
    }

    private string UploadDocument()
    {
        if (tbDPic.HasFile == false)
        { SystemTools.PopMsg("数据为空，不能上传");
        return null;
    }
    string tfname = tbDPic.PostedFile.FileName;
    string filename = SystemTools.CreateFileNameByDataTime() +tfname.Substring(tfname.LastIndexOf("."));
    filename = "img/" + filename;
        string FullName = Server.MapPath("../"+filename);
        if (File.Exists(FullName))
        {
            SystemTools.PopMsg("文件已经存在，不能上传");
            return null;
        }
        try
        {
            tbDPic.SaveAs(FullName);
            return filename;
        }
        catch (Exception ex)
        {
            SystemTools.PopMsg("文件上传失败");
            return null;
        }
    }
}
